package com.imau.brand.service.impl;

import com.imau.brand.bean.Brand;
import com.imau.brand.common.entity.PageEntitry;
import com.imau.brand.mapper.BrandMapper;
import com.imau.brand.service.BrandService;
import com.imau.brand.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class BrandServiceImpl implements BrandService {

    private SqlSession sqlSession;
    /**初始化持久层对象*/
    private BrandMapper brandMapper;

    public BrandServiceImpl()
    {
        sqlSession = SqlSessionUtil.getSqlSession();
        brandMapper = sqlSession.getMapper(BrandMapper.class);
    }
    @Override
    public boolean saveBrand(Brand brand) {
        int row = brandMapper.addBrand(brand);
        return row > 0;
    }

    @Override
    public boolean editBrand(Brand brand) {
        return brandMapper.updateBrand(brand) > 0;
    }

    @Override
    public boolean dleBrand(Integer id) {
        return brandMapper.deleteById(id) > 0;
    }

    @Override
    public Brand findById(Integer id) {
        return  brandMapper.selectById(id);
    }

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    @Override
    public boolean deleteBatchByIds(Integer[] ids) {
        return  brandMapper.deleteBatchByIds(ids) > 0 ;
    }

    @Override
    public Integer selectTotal() {
        return brandMapper.selectTotal();
    }

    @Override
    public PageEntitry<Brand> selectByPage(Integer currentPage, Integer pageSize) {
        //1.创建Page对象
        PageEntitry<Brand> pageEntitry = new PageEntitry<>();
        //2.查询总记录数
        Integer total = brandMapper.selectTotal();
        //3.查询记录
        List<Brand> brands = brandMapper.selectByPage((currentPage - 1) * pageSize, pageSize);
        //4.设置page属性
        pageEntitry.setTotal(total);
        pageEntitry.setCurrentPage(currentPage);
        pageEntitry.setPageSize(pageSize);
        pageEntitry.setRows(brands);
        return pageEntitry;
    }

    @Override
    public Integer selectTotalByCondition(Brand brand) {
        return brandMapper.selectTotalByCondition(brand);
    }

    @Override
    public PageEntitry<Brand> selectPageByCondition(Integer currentPage, Integer pageSize, Brand brand) {
        //1.创建Page对象
        PageEntitry<Brand> pageEntitry = new PageEntitry<>();
        //2.查询总记录数
        Integer total = brandMapper.selectTotalByCondition(brand);
        //3.查询记录
        List<Brand> brands = brandMapper.selectPageByCondition((currentPage - 1) * pageSize, pageSize,brand);
        //4.设置page属性
        pageEntitry.setTotal(total);
        pageEntitry.setCurrentPage(currentPage);
        pageEntitry.setPageSize(pageSize);
        pageEntitry.setRows(brands);
        return pageEntitry;
    }
}
